#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Licensed under GPL v2
# Copyright 2008, Aşkın Yollu <askin@askin.ws>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.
#

import os, shutil, sys
from distutils.core import setup
from distutils.command.build import build
from distutils.command.clean import clean

PRJ1 = "kde4_resize"
PRJ2 = "kde4_rotate"
PRJ  = "kde4-convert-image"
PRJNAME = "kde4-convert-image"
PRJNAME1 = "kde4-resize"
PRJNAME2 = "kde4-rotate"
PRJM = __import__(PRJ1)

try:
    import PyQt4
except:
    print "\033[31mWarning: You have to install PyQt4 on your system\033[0m"

def compileui(path, uiFile, PRJ):
    compiled = os.system("pyuic4 %s%s.ui -o %s/ui_%s.py" % (path, uiFile, PRJ, uiFile))
    if compiled == 0:
        print "Compiled %s%s.ui -> %s/ui_%s.py" % (path, uiFile, PRJ, uiFile)
    else:
        print "\033[31mWarning: Failed compiling %s%s.ui, pyuic4 didn't work\033[0m" % (path, uiFile)

def compileqrc(path, qrcFile, PRJ):
    compiled = os.system("pyrcc4 %s%s.qrc -o %s/%s_rc.py" % (path, qrcFile, PRJ, qrcFile))
    if compiled == 0:
        print "Compiled %s%s.qrc -> %s/%s_rc.py" % (path, qrcFile, PRJ, qrcFile)
    else:
        print "\033[31mWarning: Failed compiling %s%s.qrc, pyrcc4 didn't work\033[0m" % (path, qrcFile)

class myClean(clean):
    def run(self):
        clean.run(self)
        try:
            os.remove("MANIFEST")
            print "removed MANIFEST"
        except:pass
        try:
            shutil.rmtree("build")
            print "removed build"
        except:pass
        try:
            for file in os.listdir(PRJ1):
                if file[-4:] == ".pyc":
                    os.remove(os.path.join(PRJ1, file))
            for file in os.listdir(PRJ2):
                if file[-4:] == ".pyc":
                    os.remove(os.path.join(PRJ2, file))
            print "removed *.pyc"
        except:pass

class myBuild(build):
    def run(self):
        build.run(self)
        try:
            # dirty solution
            compileui("ui/", "resize", PRJ1)
            compileui("ui/", "rotate", PRJ2)
        except TypeError:
            print "No .ui files to compile"
        TSFile = "kde4_resize"
        if os.system("lrelease-qt4 data/%s_tr_TR.ts -qm data/%s_tr_TR.qm" % (TSFile, TSFile)) == 0:
            print "Compiled data/%s_tr_TR.ts -> data/%s_tr_TR.qm" % (TSFile, TSFile)
        else:
            print "\033[31mWarning: Failed compiling data/%s_tr_TR.ts, lrelease-qt4 didn't work\033[0m" % TSFile
        TSFile = "kde4_rotate"
        if os.system("lrelease-qt4 data/%s_tr_TR.ts -qm data/%s_tr_TR.qm" % (TSFile, TSFile)) == 0:
            print "Compiled data/%s_tr_TR.ts -> data/%s_tr_TR.qm" % (TSFile, TSFile)
        else:
            print "\033[31mWarning: Failed compiling data/%s_tr_TR.ts, lrelease-qt4 didn't work\033[0m" % TSFile
        for PRJ in (PRJ1, PRJ2):
            for qrc in (("data/", PRJ),):
                compileqrc(qrc[0], qrc[1], PRJ)

datas = [('share/applications', ['data/%s.desktop' % PRJNAME])]

setup(name = PRJNAME,
      version = PRJM.__version__,
      description = "KDE4 Image Converter",
      author = "Aşkın Yollu",
      author_email = "askin@askin.ws",
      license = "GNU General Public License, Version 2",
      url = "http://askin.ws",
      packages = [PRJ1, PRJ2],
      data_files = datas,
      scripts = ['data/%s' % PRJNAME1, 'data/%s' % PRJNAME2],
      cmdclass = {"build" : myBuild,
                  "clean" : myClean}
      )
